<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se tem permissao
validaPermissao(3);

// Verifica se houve submit
if (isset($_POST['submit']))
{
    // Checa integridade dos tickets ranges
	for ($c=0; $c < count($_POST['ticket_starts']); $c++)
	{
        if ($_POST['ticket_starts'][$c] == '' || $_POST['ticket_ends'][$c] == '')
        {
            header( "Location: location_new.php?id={$_POST['id']}&msg=" . urlencode("Could not update location: Empty ticket range was given.") );
            die();
        }
    }

	// Cria contato da locacao
	query_bd("INSERT INTO contacts VALUES " .
	"(NULL, " . ($_POST['location_name']? "'{$_POST['location_name']}'" : "NULL") .
	", " . ($_POST['location_number']? "{$_POST['location_number']}" : "NULL") .
	", " . ($_POST['location_street']? "'{$_POST['location_street']}'" : "NULL") .
	", " . ($_POST['location_complement']? "'{$_POST['location_complement']}'" : "NULL") .
	", " . ($_POST['location_city']? "'{$_POST['location_city']}'" : "NULL") .
	", " . ($_POST['location_state']? "'{$_POST['location_state']}'" : "NULL") .
	", " . ($_POST['location_zipcode']? "'{$_POST['location_zipcode']}'" : "NULL") .
	", " . ($_POST['location_phone']? "'{$_POST['location_phone']}'" : "NULL") .
	", " . ($_POST['location_cellphone']? "'{$_POST['location_cellphone']}'" : "NULL") .
	", " . ($_POST['location_email']? "'{$_POST['location_email']}'" : "NULL") . " )");

	$locationcontact = mysql_insert_id(); //pega ultimo id inserido

	// Cria contato do manager
	query_bd("INSERT INTO contacts VALUES " .
	"(NULL, " . ($_POST['manager_name']? "'{$_POST['manager_name']}'" : "NULL") .
	", " . ($_POST['manager_number']? "{$_POST['manager_number']}" : "NULL") .
	", " . ($_POST['manager_street']? "'{$_POST['manager_street']}'" : "NULL") .
	", " . ($_POST['manager_complement']? "'{$_POST['manager_complement']}'" : "NULL") .
	", " . ($_POST['manager_city']? "'{$_POST['manager_city']}'" : "NULL") .
	", " . ($_POST['manager_state']? "'{$_POST['manager_state']}'" : "NULL") .
	", " . ($_POST['manager_zipcode']? "'{$_POST['manager_zipcode']}'" : "NULL") .
	", " . ($_POST['manager_phone']? "'{$_POST['manager_phone']}'" : "NULL") .
	", " . ($_POST['manager_cellphone']? "'{$_POST['manager_cellphone']}'" : "NULL") .
	", " . ($_POST['manager_email']? "'{$_POST['manager_email']}'" : "NULL") . " )");

	$managercontact = mysql_insert_id(); //pega ultimo id inserido

	// Cria locacao
	query_bd("INSERT INTO locations VALUES ".
	"(NULL, {$locationcontact}, {$managercontact}, '{$_POST['workingtime']}', " .
	"{$_POST['valet_payrate']}, {$_POST['manager_payrate']}, {$_POST['insurance_tax']}, " .
	($_POST['tip_percent']/100.0) . ", TRUE, " .
	($_POST['obs']? "'" . mysql_real_escape_string($_POST['obs']) . "'" : "NULL") . ")");

	$location_id = mysql_insert_id(); //pega ultimo id inserido

    // Insere ticket_ranges
	for ($c=0; $c < count($_POST['ticket_starts']); $c++)
	{
		query_bd("INSERT INTO ticket_ranges VALUES " .
		"(NULL, {$location_id}, {$_POST['ticket_starts'][$c]}, {$_POST['ticket_ends'][$c]} )");
	}

	header( "Location: location.php?id={$location_id}&msg=New location added to the system." );
	die();
}

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - New Location');
adicionar_js('input-format.js');
adicionar_js('add-remove.js');
pagina_head();

?>
<script type='text/javascript' language="JavaScript">

function addTicketRange() {
    var html = '<span ticket_range_' + ticket_range + '>' +

	'<label for=ticket_start_' + ticket_range + '>Start ticket:</label> ' +
	'$<input type=text id=ticket_start_' + ticket_range + ' name="ticket_starts[]" value="" ' +
	'title="Please enter a value greater than zero" onChange="format_int(this)" size=6 maxlength=6 > ' +

	'<label for=ticket_end_' + ticket_range + '>End ticket:</label> ' +
	'<input type=text id=ticket_end_' + ticket_range + ' name="ticket_ends[]" value="" ' +
	'title="Please enter a value greater than zero" onChange="format_int(this)" size=6 maxlength=6 > ' +

	'<a href="" title="Remove this ticket range entry" onclick="javascript:removeElement(\'ticket_range_' + ticket_range + '\'); return false;">' +
    '<small>Remove</small></a><br>' +
    '</span>'

	addElement('ticket_ranges', 'span', 'ticket_range_' + ticket_range, html);
	ticket_range++;
};

var ticket_range = 0;

</script>
<?php


// Formulario para entrar com os dados da nova locacao
echo "<form id=location_new class=big_form method=POST action='location_new.php'>";
echo "<h1>New Location</h1><br>";
echo "<label for=location_name>Location name:</label> <input type=TEXT id=location_name name=location_name value='' size=40 maxlength=80><br>";
echo "<label for=workingtime>Working time:</label> <input type=TEXT id=workingtime name=workingtime value='' size=40 maxlength=80><br>";

echo "<h2><h2>Tickets range</h2><br>";
echo "<div id=ticket_ranges>";
echo "</div>";
echo "<br><input type='button'  value='Add Ticket Range' onclick='addTicketRange();'>";

echo "<hr><h2>Location Contact</h2><br>";
echo "<label for=location_number>Number:</label> <input type=TEXT id=location_number name=location_number value='' size=8 onChange='format_int(this)'>";
echo " <label for=location_street>Street:</label> <input type=TEXT id=location_street name=location_street value='' size=30 maxlength=50>";
echo " <label for=location_complement>Complement:</label> <input type=TEXT id=location_complement name=location_complement value='' size=10 maxlength=30><br>";
echo "<label for=location_city>City:</label> <input type=TEXT id=location_city name=location_city value='' size=10 maxlength=30>";
echo " <label for=location_state>State:</label> <input type=TEXT id=location_state name=location_state value='' size=8 maxlength=10>";
echo " <label for=location_zipcode>Zipcode:</label> <input type=TEXT id=location_zipcode name=location_zipcode value='' size=6 maxlength=10><br>";
echo "<label for=location_phone>Phone:</label> <input type=TEXT id=location_phone name=location_phone value='' size=12 maxlength=20>";
echo " <label for=location_cellphone>Cellphone:</label> <input type=TEXT id=location_cellphone name=location_cellphone value='' size=12 maxlength=20><br>";
echo "<label for=location_email>Email:</label> <input type=TEXT id=location_email name=location_email value='' size=20 maxlength=30>";

echo "<hr><h2>Manager Contact</h2><br>";
echo "<label for=manager_name>Manager name:</label> <input type=TEXT id=manager_name name=manager_name value='' size=40 maxlength=80><br>";
echo "<label for=manager_number>Number:</label> <input type=TEXT id=manager_number name=manager_number value='' size=8 onChange='format_int(this)'>";
echo " <label for=manager_street>Street:</label> <input type=TEXT id=manager_street name=manager_street value='' size=30 maxlength=50>";
echo " <label for=manager_complement>Complement:</label> <input type=TEXT id=manager_complement name=manager_complement value='' size=10 maxlength=30><br>";
echo "<label for=manager_city>City:</label> <input type=TEXT id=manager_city name=manager_city value='' size=10 maxlength=30>";
echo " <label for=manager_state>State:</label> <input type=TEXT id=manager_state name=manager_state value='' size=8 maxlength=10>";
echo " <label for=manager_zipcode>Zipcode:</label> <input type=TEXT id=manager_zipcode name=manager_zipcode value='' size=6 maxlength=10><br>";
echo "<label for=manager_phone>Phone:</label> <input type=TEXT id=manager_phone name=manager_phone value='' size=12 maxlength=20>";
echo " <label for=manager_cellphone>Cellphone:</label> <input type=TEXT id=manager_cellphone name=manager_cellphone value='' size=12 maxlength=20><br>";
echo "<label for=manager_email>Email:</label> <input type=TEXT id=manager_email name=manager_email value='' size=20 maxlength=30>";

echo "<hr><h2>Location Information</h2><br>";
echo "<label for=manager_payrate>Manager payrate:</label> <input type=text id=manager_payrate name=manager_payrate value='0.0' size=5 onChange='format_money(this)'><br>";
echo "<label for=valet_payrate>Valet payrate:</label> <input type=text id=valet_payrate name=valet_payrate value='0.0' size=5 onChange='format_money(this)'><br>";
echo "<label for=insurance_tax>Insurance tax per car:</label> <input type=text id=insurance_tax name=insurance_tax size=5 value='0.0' onChange='format_money(this)'><br>";
echo "<label for=tip_percent>Tip percent income:</label> <input type=text id=tip_percent name=tip_percent size=5 value='0.0' onChange='format_money(this)'>%<br>";

echo "<label for=obs>Observations:</label><br><textarea id=obs name=obs cols=40 rows=5>";
echo "</textarea>";
echo "<hr>";
echo "<input type=SUBMIT name=submit value='Submit'>";

echo "</form>";

// Finaliza pagina
pagina_fim();
?>
